#ifndef STRATEGY_SYS_H_
#define STRATEGY_SYS_H_
#pragma once
#include <QtGui>
#include "utilities_all.h"






//
//typedef struct StrategyComData
//{
//	unsigned int varsize;
//	char* pvardata;
//
//}StrategyComData;

class CStrategySys: public QObject
{
	Q_OBJECT

public:
	//These part interfaces used a way of NVI consist of 
	//1. interface
	//2. non virtual private prefix function called by above interface
	//3. pure virtual private function that would be rewrote by derived class

	//these interface is part 1.


	CStrategySys(string &threadid);

	virtual ~CStrategySys();

	//------------------------called by CFrame MFC_Frame()--------------------------------------------------
	bool Sys_Init(BarInfo* pbar, char* puserdata = NULL);

	bool Sys_Destroy(BarInfo* pbar);

	bool Sys_Edit(BarInfo* pbar, char* puserdata);

	void Sys_Getmapuserbar(map<string, vector<BarSeries*>>& mapuserbar);

	void Sys_StrategyCom(unsigned int size, char* pdata);

	//------------------------called by CFrame Md_Frame()--------------------------------------------------

	void Sys_Run(BarData* ptickdata, TThostFtdcInstrumentIDType driveinst);

	//------------------------called by CFrame Td_Frame()--------------------------------------------------

	void Sys_OnOrder(CThostFtdcOrderField *pOrder);

	void Sys_OnTrade(CThostFtdcTradeField *pTrade);

	void Sys_OnCancelOrder(CThostFtdcOrderField *pOrder);

	void Sys_OnCancelOrderFailed(CThostFtdcInputOrderActionField *pCancelOrderFailed);

	


public:
	//These part interfaces NOT using NVI(because these interface don't call virtual func rewrote by derived class), 
	//They are called by CFrame directly 


	//------------------------called by CFrame Timer_Frame()--------------------------------------------------

	void Sys_OnStatistic();

	//------------------------called by CFrame OnQry...() related interface--------------------------------------------------

	void Sys_OnQryInstrument(CThostFtdcInstrumentField *pInstrument, int reqid);

	void Sys_OnQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, int rspid);

	void Sys_OnQrytCommissionRate(CThostFtdcInstrumentCommissionRateField *pInstrumentCommissionRate, int rspid);


protected:

	//these functions are ONLY called by derived class, so not defined as interface.

	bool LimitOrder(const char* inst, double price, char director, char offset, int volumn);

	bool LimitOrder(const char* inst, double price, char director, char offset, int volumn, TThostFtdcOrderRefType* porderref);

	bool LimitOrder(const char* inst, double baseprice, unsigned int jump, char director, char offset, int volumn, TThostFtdcOrderRefType* porderref);

	bool CancelOrder(TThostFtdcInstrumentIDType* instrument, TThostFtdcOrderRefType* orderref, TThostFtdcExchangeIDType* exchangeId, TThostFtdcOrderSysIDType* ordersysId);

	//this is not normal interface for user strategy, because user should defined data struct 
	//to get the map from order to sysid and exchangeid.
	//defined this only for user convineous not defined map again. 
	bool Getmap(char* inst, TThostFtdcOrderRefType* porder, OrderSysMap* &poutmap);

	bool Getmap_New(char* inst, TThostFtdcOrderRefType orderref, OrderSysMap* &poutmap);

	bool QryInstrument(char* inst, CThostFtdcInstrumentField* &pInstrument/*out*/);

	bool QryQryTradingAccount(CThostFtdcTradingAccountField* &pTradingAccount);

	bool QrytCommissionRate(char* inst, CThostFtdcInstrumentCommissionRateField* &pInstrumentCommissionRate/*out*/);
private:

	//these non virtual private function are part 2 (prefix function inside the interface )

	bool Sys_InitTrademan(char* inst);

	bool Sys_InitInstField(char* inst);

	bool Sys_InitCommisionRate(char* inst);

	bool Sys_DestroyTrademan(char* inst);

	bool Sys_DestroyInstField(char* inst);

	bool Sys_DestroyCommisionRate(char* inst);

private:

	//these pure virtual private function are part 3 (also inside the interface but called after above prefix function)

	virtual bool Init(BarInfo* pbar, map<string, vector<BarSeries*>>& mapuserbar, char* puserdata = NULL) = 0;

	virtual bool Destroy(BarInfo* pbar, map<string, vector<BarSeries*>>& mapuserbar) = 0;

	virtual bool Edit(BarInfo* pbar, char* puserdata) = 0;

	virtual void Run(BarData* ptickdata, TThostFtdcInstrumentIDType driveinst) = 0;

	virtual void OnOrder(CThostFtdcOrderField *pOrder) = 0;

	virtual void OnTrade(CThostFtdcTradeField *pTrade) = 0;

	virtual void OnCancelOrder(CThostFtdcOrderField *pOrder) = 0;

	virtual void OnCancelOrderFailed(CThostFtdcInputOrderActionField *pCancelOrderFailed) = 0;

	virtual void OnStrategyCom(unsigned int size, char* pdata) = 0;



signals:

	void SignalSysMd(BarData* pData);

	


protected:

	map<string, CThostFtdcInstrumentField*> m_mapinstfield;


private:
	//only used for derived class LimitOrder and CancelOrder function;
	//unsigned int m_tindex;
	string m_threadid;

	map<string, vector<BarSeries*>> m_mapuserbar;

	map<string, OrderSysMapSeries*> m_maptrademan;

	//map<string, CThostFtdcInstrumentCommissionRateField*> m_mapcommisionrate;

	//map<string, CThostFtdcInstrumentField*> m_mapinstfield;

	int m_reqid;

	int m_rspid;

	HANDLE m_han;

	char* m_qry_pointor;


/*******************************************static *******************************************************/
private:

	static CTrade* m_pTrade;

	

public:
	

	static bool Login_TradingDay_InstRead(char* pborkeid, char* puserid, char* ppassword,  char* pTDFront, unsigned int flag);

	static bool Logout();

	//static bool GetTdConnectState();


	//static map<string, vector<pair<string, double> > >* GetMapPdt();
	//static map<string, vector<pair<string, double> > > m_mapPdt;

	static map<string, vector<InstInfo> > m_instRead;
	static map<string, vector<InstInfo> > m_instWrite;


	static bool ConvertXml(char* sourceInst, char* destInst);

	static bool ConvertInst(char* sourceInst, char* destInst);

/*********************************************************************************************************/

};


#endif